import { makeUrl } from './router.js'
import { $msg } from './msg.js'
import { WX_APP_ORGINAL_ID } from '@/config'

/**
 * 预览图片
 * */
export function $previewImg(urls, current) {
	if (!Array.isArray(urls)) {
		urls = [urls]
		current = 0
	}
	uni.previewImage({
		urls,
		current,
		fail(e) {
			console.log(e);
		}
	})
}

/**
 * 打电话
 */
export function $phoneCall(phoneNumber) {
	uni.makePhoneCall({ phoneNumber })
}

/**
 * 显示地图，传入经纬度
 */
export function $showMap({ lat, lng }) {
	uni.openLocation({
		latitude: parseFloat(lat),
		longitude: parseFloat(lng),
		fail(e) {
			console.log(e);
		}
	})
}

/**
 * uni-app开发的APP端拉起小程序
 * path：指定小程序的页面路径
 */
export function $launchMiniProgram(path) {
	plus.share.getServices(shareList => {
		let shareWx = shareList.find(val => val.id == 'weixin')
		if (shareWx) {
			$msg('正在唤起小程序')
			//id是小程序原始ID而不是appid
			shareWx.launchMiniProgram({ id: WX_APP_ORGINAL_ID, path })
		} else
			$msg('未安装微信,无法打开小程序')

	}, e => $msg('唤起小程序失败'))
}


export function $subscribeMsg() {
	// #ifdef MP-WEIXIN
	let tempId = 'DXyWDfLJPDDrhyJ6wOnE8Pm_pKDFQhjzLa9ucdFkavE'
	return new Promise((resolve, reject) => {
		uni.requestSubscribeMessage({
			tmplIds: [tempId],
			success: async res => {
				// if(res[tempId] === 'accept')
				//不管是否调用订阅，都可以买
				resolve(res)
			},
			fail: err => reject(err)
		})
	})
	// #endif
}
